<template>
  <el-form
    ref="loginForm"
    class="login-form"
    status-icon
    :rules="loginRules"
    :model="loginForm"
    label-width="0"
  >
    <el-form-item
      v-if="isShowTenantCode"
      prop="tenantCode"
    >
      <el-input
        v-model="loginForm.tenantCode"
        size="small"
        autocomplete="on"
        :placeholder="$t('login.tenantCode')"
        @keyup.enter.native="handleLogin"
      >
        <i
          slot="prefix"
          class="icon-canshu"
        />
      </el-input>
    </el-form-item>
    <el-form-item prop="username">
      <el-input
        v-model="loginForm.username"
        size="small"
        autocomplete="on"
        :placeholder="$t('login.username')"
        @keyup.enter.native="handleLogin"
      >
        <i
          slot="prefix"
          class="icon-yonghu"
        />
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        v-model="loginForm.password"
        size="small"
        autocomplete="on"
        :type="passwordType"
        :placeholder="$t('login.password')"
        @keyup.enter.native="handleLogin"
      >
        <i
          slot="suffix"
          class="el-icon-view el-input__icon"
          @click="showPassword"
        />
        <i
          slot="prefix"
          class="icon-mima"
        />
      </el-input>
    </el-form-item>
    <el-form-item prop="code">
      <el-row :span="24">
        <el-col :span="16">
          <el-input
            v-model="loginForm.code"
            size="small"
            auto-complete="off"
            :placeholder="$t('login.code')"
            @keyup.enter.native="handleLogin"
          >
            <i slot="prefix" class="icon-yanzhengma" />
          </el-input>
        </el-col>
        <el-col :span="8">
          <div class="login-code">
            <img
              :src="checkImg||loginForm.image"
              class="login-code-img"
              @click="refreshCode"
            >
          </div>
        </el-col>
      </el-row>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        size="small"
        class="login-submit"
        @click.native.prevent="handleLogin"
      >{{ $t('login.submit') }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'

export default {
  name: 'Userlogin',
  props: [],
  data() {
    return {
      loginForm: {
        username: '',
        tenantCode: '',
        password: '',
        // 验证码的值
        code: '',
        // 预加载白色背景
        image: ''
      },
      checked: false,
      loginRules: {
        username: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }],
        tenantCode: [{
          required: true,
          message: '请输入租户编码',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入验证码',
          trigger: 'blur'
        }]
      },
      passwordType: 'password',
      isShowTenantCode: true,
      website: {}
    }
  },

  computed: {
    ...mapGetters(['checkImg'])
  },
  watch: {
    captcha(newVal) {
      console.log(newVal)
    }
  },
  created() {
    this.refreshCode()
  },
  mounted() {
    // if (this.website.isTenant) {
    //   this.$store
    //     .dispatch('GetTenantCode').then((res) => {
    //       this.$store.commit('SET_LOGO', res.logo)
    //       if (res.tenantCode) {
    //         this.loginForm.tenantCode = res.tenantCode
    //         this.isShowTenantCode = false
    //       } else {
    //         this.isShowTenantCode = true
    //       }
    //     })
    // }
  },
  methods: {
    ...mapActions('user', ['getCaptcha']),
    refreshCode() {
      this.getCaptcha()
    },
    showPassword() {
      this.passwordType === ''
        ? (this.passwordType = 'password')
        : (this.passwordType = '')
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '登录中,请稍后。。。',
            spinner: 'el-icon-loading'
          })

          // this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            loading.close()
            // this.loading = false
          }).catch(() => {
            this.loading = false
            loading.close()
          })
          // if (valid) {
          // } else {
          //   console.log('error submit!!')
          //   return false
          // }

          // this.$store
          //   .dispatch('LoginByUsername', this.loginForm)
          //   .then(() => {
          //     this.$router.push({
          //       path: this.tagWel.value
          //     })
          //     loading.close()
          //   })
          //   .catch(err => {
          //     loading.close()
          //     console.log(err)
          //   })
        }
      })
    }
  }
}
</script>

<style>
</style>
